package godi

import (
	"gitee.com/ichub/goconfig/common/base/baseiface"
	"gitee.com/ichub/goconfig/common/base/baseutils/fileutils"
	"gitee.com/ichub/goconfig/common/base/baseutils/goutils"
	"gitee.com/ichub/goconfig/common/base/baseutils/jsonutils"
	"gitee.com/ichub/goconfig/common/ichublog"
	"gitee.com/ichub/goconfig/godi/godifactroy"
	"gitee.com/ichub/goconfig/godi/internal/didto"
	"gitee.com/ichub/goconfig/godi/internal/dimodel/single"
	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/suite"
	"go/build"
	"testing"
)

type TestDiFactoySuite struct {
	suite.Suite
	DiFactroy *godifactroy.DiFactroy
	rootdir   string
}

func (this *TestDiFactoySuite) SetupTest() {
	logrus.Info("godifactroy...")
	ichublog.InitLogrus()
	this.rootdir = fileutils.FindRootDir()
	var i baseiface.ISingleton = nil
	logrus.Info(i)
	this.DiFactroy = godifactroy.NewDiFactroy()
}

func TestDiFactoySuites(t *testing.T) {
	suite.Run(t, new(TestDiFactoySuite))
}

func (this *TestDiFactoySuite) Test001_FindGofiles() {
	this.DiFactroy.FindGoFiles()

	logrus.Info("basedi=", jsonutils.ToJsonPretty(this.DiFactroy))
}

func (this *TestDiFactoySuite) Test002_MakeDiOne() {
	this.DiFactroy.ParseAll()
	var fileinfo = this.DiFactroy.Parse("./godifactroy/di_factroy.go")
	fileinfo.StructInfos[0].ForceBuild = true
	this.DiFactroy.MakeBatch(fileinfo.StructInfos[0])
}

// 解析依赖关系
func (this *TestDiFactoySuite) Test003_MakeDiAll() {

	this.DiFactroy.MakeDiAll()

}

func (this *TestDiFactoySuite) Test004_MakeDiS1_MemberBaseEntity() {

	var fileinfo = this.DiFactroy.Parse("./godifactroy/s1.go")
	fileinfo.StructInfos[0].ForceBuild = true
	this.DiFactroy.MakeBatch(fileinfo.StructInfos[0])

}

func (this *TestDiFactoySuite) Test005_ParseAll() {

	this.DiFactroy.ParseAll()
	var cf = this.DiFactroy.FindSome("IchubConfig")
	logrus.Error("\r\n", cf.PathFile)
	logrus.Info(cf.ToString())
}

// find is not support multi entity
func (this *TestDiFactoySuite) Test007_FindSingle() {
	var bean1 = single.FindBeanSingleEntity()

	bean1.Name = "111"
	var bean2 = single.FindBeanSingleEntity()
	var bean3 = single.FindBeanSingleEntity()
	bean2.Name = "222"
	logrus.Info(bean1, bean2, bean3)
	this.Equal(bean1.Name == bean2.Name, true)

}

func (this *TestDiFactoySuite) Test008_MakeDi() {
	var dto = didto.FindBeanDiDto()
	dto.PkgName = "didto"
	dto.StructName = "DiDto"
	this.DiFactroy.MakeDi(dto)

}

func (this *TestDiFactoySuite) Test009_FindFs() {
	this.DiFactroy.FindGoFiles()
	for _, v := range this.DiFactroy.FileInfoDtos {
		this.DiFactroy.Parse(v.PathFile)

	}
}

func (this *TestDiFactoySuite) Test010_MakeDiOne() {

	var fileinfo = this.DiFactroy.Parse(this.rootdir + "/common/ichubcontext/ichub_common_context.go")
	this.DiFactroy.MakeBatch(fileinfo.StructInfos[0])
}
func (this *TestDiFactoySuite) Test011_goutilsErr() {

	var fileinfo = this.DiFactroy.Parse(this.rootdir + "/common/ichubcontext/ichub_common_context.go")
	for true {
		goutils.Error(fileinfo.StructInfos[0])
	}
}
func (this *TestDiFactoySuite) Test012_goutilsErr() {

	var fileinfo = this.DiFactroy.Parse(this.rootdir + "/common/ichubcontext/ichub_common_context.go")

	goutils.Info(fileinfo.StructInfos[0])

}

func (this *TestDiFactoySuite) Test012_goutilsPkg() {
	pkg, err := build.Default.Import("github.com/sirupsen/goconfig", "", build.FindOnly)
	logrus.Info(pkg.Dir, err)

	pkg, err = fileutils.ImportPkg("github.com/sirupsen/goconfig")
	//var pp, _ = importer.Default().Import("gitee.com/ichub/goconfig/common/base/baseiface")
	logrus.Info(jsonutils.ToJsonPretty(pkg))
}
